﻿#include "PowerUp.h"
//#include "Global.h"
#include "Resource.h"

PowerUp::PowerUp(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction) : Object(x, y)
{
	this->SetID(ID);
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\item_32.png"), 150, 0);

	_xOld = _x;
	_yOld = _y;

	_vx = 0;
	_vy = 0;
	_accelY = 0;

	isMushroom = true;

	object_State = Object_State;
	direction = Direction;
	if (this->GetID() == 43)		//nam mang
	{
		if (object_State == IS_IDLE)
		{
			_sprite->SetFrame(7,7);	//frame an
		} 
		else if (object_State == IS_MOVING)
		{
			isMushroom = true;
			_sprite->SetFrame(1,1);
			if (direction == 1) //sang trai
			{
				_vx = -0.2;
			} 
			else if (direction == 2) //sang trai
			{ 
				_vx = 0.2;
			}
			
		}
	} 
	else if (this->GetID() == 45)	//nam to, hoa
	{
		if (object_State == IS_IDLE)
		{
			_sprite->SetFrame(7,7);	//frame an
		} 
		else if (object_State == IS_MOVING)
		{
			isMushroom = true;
			_sprite->SetFrame(0,0);
			if (direction == 1) //sang trai
			{
				_vx = -0.2;
			} 
			else if (direction == 2) //sang trai
			{ 
				_vx = 0.2;
			}
			 
		}
		else if (object_State == IS_READY)
		{
			isMushroom = false;
			_sprite->SetFrame(2,2);
		}
	}
	
	


	//switch(this->GetID())
	//{
	//case 43:
	//case 45:
	//	object_State = IS_IDLE;
	//	_sprite->SetFrame(7,7);	//frame an
	//	break;

	////Load save map
	//case 431: 	//nam mang mau xanh khi di chuyen sang trai
	//	{
	//		isMushroom = true;
	//		_sprite->SetFrame(1,1);
	//		//ChangeState(IS_MOVING);
	//		object_State = IS_MOVING;
	//		_vy=0;

	//		_vx=-0.200;

	//		//moi them
	//		SetID(431);	 
	//	}
	//	break;
	//case 432: 	//nam mang mau xanh khi di chuyen sang phai
	//	{
	//		isMushroom = true;
	//		_sprite->SetFrame(1,1);
	//		//ChangeState(IS_MOVING);
	//		object_State = IS_MOVING;
	//		_vy=0;

	//		_vx=0.200;

	//		//moi them
	//		SetID(432);	 
	//	}
	//	break;
	//case 491: 	//nam mang mau xanh khi di chuyen sang trai
	//	{
	//		isMushroom = true;
	//		_sprite->SetFrame(0,0);
	//		//ChangeState(IS_MOVING);
	//		object_State = IS_MOVING;
	//		_vy=0;
	//		
	//		_vx=-0.200;

	//		//moi them
	//		SetID(491);	//nam to
	//	}
	//	break;
	//case 492: 	//nam mang mau xanh khi di chuyen sang phai
	//	{
	//		isMushroom = true;
	//		_sprite->SetFrame(0,0);
	//		//ChangeState(IS_MOVING);
	//		object_State = IS_MOVING;
	//		_vy=0;
	//		 
	//		_vx=0.200;

	//		//moi them
	//		SetID(492);	//nam to
	//	}
	//	break;
	//case 50:	//hoa khi hien
	//	{
	//		isMushroom = false;
	//		_sprite->SetFrame(2,2);
	//		//ChangeState(IS_READY);
	//		//_y=this->GetBond().bottom - 32;//container->GetY()-40;//TILE_SIZE;
	//		object_State = IS_READY;
	//		//moi them
	//		SetID(50);	//la hoa
	//	}
	//	break;
	//}

	//if (this->GetID() == 43)	//nam mang mau xanh
	//{
	//	object_State = IS_IDLE;
	//	_sprite->SetFrame(7,7);	//frame an
	//}
	//else if (this->GetID() == 45)	//nam lon mau do, hoa
	//{
	//	object_State = IS_IDLE;
	//	_sprite->SetFrame(7,7);	//frame an
	//}

	//

	//

	//_xOld = _x;
	//_yOld = _y;

	//_vx = 0;
	//_vy = 0;
	//_accelY = 0;

	//isMushroom = true;
	//container = NULL;
	//isCheckedContainer = false;

	////Load save map
	//if (this->GetID() == 48)	//nam mang mau xanh khi di chuyen
	//{
	//	_sprite->SetFrame(1,1);
	//	ChangeState(IS_MOVING);
	//}
	//else if (this->GetID() == 49)	//nam lon mau do khi di chuyen
	//{
	//	isMushroom = true;
	//	_sprite->SetFrame(0,0);
	//	//ChangeState(IS_MOVING);
	//	object_State = IS_MOVING;
	//	_vy=0;
	//	if(_vx==0.100)
	//		_vx=-0.100;
	//	else 
	//		_vx=0.200;
	//	
	//	//moi them
	//	SetID(49);	//nam to
	//}
	//else if (this->GetID() == 50)	//hoa khi hien
	//{
	//	isMushroom = false;
	//	_sprite->SetFrame(2,2);
	//	ChangeState(IS_READY);
	//}
}

PowerUp::~PowerUp()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

void PowerUp::Render()
{
	if (object_State != IS_REMOVE)
	{
		_sprite->Render(_x, _y);
	}
	
}

OBJECT_TYPE PowerUp::GetTypeObject()
{
	
	return OBJECT_LAND;
}

void PowerUp::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void PowerUp::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	switch(object_State)
	{
	case IS_IDLE:
		CheckObjects(staObjs,dynObjs);
		break;
	case IS_ACTIVING:
		//Move(gameTime,staObjs,dynObjs);
		_x+=_vx*gameTime;
		_y+=_vy*gameTime+1/2*_accelY*gameTime*gameTime;
		if(_y< this->GetBond().bottom - 32-5)
			if(isMushroom==true)
			{
				if (this->GetID() == 43)	//nam mang
				{
					_sprite->SetFrame(1,1);
				}
				else						//nam lon
				{
					_sprite->SetFrame(0,0);
				}
				
				ChangeState(IS_MOVING);
			}
			else
			{
				_sprite->SetFrame(2,2);
				ChangeState(IS_READY);
			}
			break;
	case IS_MOVING:
		Move(gameTime,staObjs,dynObjs);
		break;
	case IS_READY:
		//Move(gameTime,staObjs,dynObjs);
		break;
	}

	//if (isMushroom)
	//{
	//	_sprite->SetFrame(0,0);
	//	_sprite->Update(gameTime);
	//}
	//else
	//{
	//	//_sprite->SetFrame(2,2);
	//	_sprite->Update(gameTime);
	//}
	_sprite->Update(gameTime);
	ResetRect();
}

void PowerUp::CheckObjects(list<Object*>* staObjs,list<Object*>* dynObjs)
{

	list<Object*>::iterator i;
	Object* obj;
	if(isCheckedContainer==false)
		for(i=staObjs->begin();i!=staObjs->end();i++)
		{
			obj=*i;
			if(obj->GetID()==21||obj->GetID()==25) //gach:21, gach dau hoi: 25
				if(Collision::RectVsRect(this->GetBond(),obj->GetBond()))
				{
					container=obj;
					break;
				}
		}
		//for(i=dynObjs->begin();i!=dynObjs->end();i++)
		//{
		//	obj=*i;
		//	if(obj->GetID()== 1)		//Là Mario: 1
		//		//if(obj->GetVar("isBig")==1) //neu mario da lon
		//			isMushroom = false;
		//}
		isCheckedContainer=true;
}

void PowerUp::Move(float TPF,list<Object*>* staObjs,list<Object*>* dynObjs)
{
	Object* obj;
	//Theo phương x
	_x+=_vx*TPF;
	ResetRect();
	list<Object*>::iterator i;
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		//if(obj->GetID()>=20&&obj->GetID()<=29) //Nếu là thùng, đá , gạch...
		if (obj->GetTypeObject() == OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond()) == true)	
			{
				_vx=-_vx;
				_x=_xOld;
			}
	}
	//Theo phương y
	_accelY = GRAVITY;
	_vy+=_accelY*TPF;
	_y+=_vy*TPF+1.0/2*_accelY*TPF*TPF;
	ResetRect();
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		//if(obj->ID>=20&&obj->ID<=28) //Nếu là thùng, đá , gạch...
		if (obj->GetTypeObject() ==OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
			{
				_accelY=0;
				_vy=0;
				_y=_yOld;
			}
	}

	_xOld=_x;
	_yOld=_y;

	if (_vx >= 0)
	{
		//if (GetID() == 43 || GetID() == 432) //nam mang
		//{
		//	SetID(432);
		//} 
		//else if (GetID() == 45 || GetID() == 492) //nam to, hoa
		//{
		//	SetID(492);
		//}
		//SetID(492);
		direction = 2;
	} 
	else
	{
		//if (GetID() == 43 || GetID() == 431) //nam mang
		//{
		//	SetID(431);
		//} 
		//else if (GetID() == 45 || GetID() == 491) //nam to, hoa
		//{
		//	SetID(491);
		//}
		//SetID(491);
		direction = 1;
	}
}

void PowerUp::ChangeState(int state)
{
	object_State = state;
	switch(state)
	{
	case IS_ACTIVING:
		//_vx = POWERUP_VX;
		_vy=-4.00;
		//_accelY = GRAVITY;
		//container->SetVar("isChanged",true);
		break;
	case IS_MOVING:
		_vy=0;
		if(_vx==0.100)
			_vx=-0.200;
		else 
			_vx=0.200;
 
		break;
	case IS_READY:
		_y=this->GetBond().bottom - 32;//container->GetY()-40;//TILE_SIZE;

		//moi them
		//SetID(50);	//la hoa
		break;
	//case IS_ATE:
	//	//isNeedDelete=true;
	//	break;
	case IS_FLOWER:
		isMushroom = false;
		ChangeState(IS_ACTIVING);
		break;
	case IS_REMOVE:
		//isNeedDelete=true;
		break;
	}
}